package com.superteam.mac.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.superteam.mac.base.data.DataBean;
import com.superteam.mac.base.web.action.BaseAction;
import com.superteam.mac.mode.IndexConfig;
import com.superteam.mac.mode.MacUser;
import com.superteam.mac.mode.MachineRec;
import com.superteam.mac.mode.WorkShop;
import com.superteam.mac.servlet.RealTimeUtil;
import com.superteam.mac.web.form.UserForm;

public class LoginAction extends BaseAction {

	
	public ActionForward macLogin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UserForm uform = (UserForm)form;
		MacUser user = new MacUser();
		user.setUserName(uform.getUserName());
		user.setPassWord(uform.getPassWord());
		
		MacUser oldUser = this.getUserService().findOldUser(user);
		if(oldUser == null)
		{
			request.setAttribute("error", "用户名或密码错误 请重试！");
			return mapping.findForward("error");
		}
		if(!oldUser.getPassWord().equals(user.getPassWord()))
		{
			request.setAttribute("error", "用户名或密码错误 请重试！");
			return mapping.findForward("error");
			
		}
		
		//获得机床信息 和 指标信息
		
		ServletContext sc = request.getSession().getServletContext();
		if(sc.getAttribute("machines") == null)
		{
			List<WorkShop> list = this.getWrokShopService().findWorkShopAndMachines();
			Map<String,MachineRec> shops = new HashMap<String,MachineRec>();
			for(WorkShop work:list)
			{
				List<MachineRec> recs = work.getMachines();
				for(MachineRec rec:recs)
				{
					shops.put(rec.getMachineCode(), rec);
					RealTimeUtil.machines.put(rec.getMachineCode(), rec.getMachineName());
					DataBean.MACHINES.put(rec.getMachineCode(), rec.getMachineName());
					//System.out.println(rec.getMachineCode()+"==="+rec.getMachineName());
				}
			}
			sc.setAttribute("machines", shops);
			
		}
		
		//各种状态的值
		if(sc.getAttribute("machinestauts") == null)
		{
			
			int machineTotal = this.getStateRecDAO().findMachineStautTotal();
			sc.setAttribute("machinestauts", machineTotal);
			//未连接，待机，工作
			sc.setAttribute("nonet", this.getStateRecDAO().findMachineStautByStautName("未连接"));
			sc.setAttribute("witing", this.getStateRecDAO().findMachineStautByStautName("待机"));
			sc.setAttribute("work", this.getStateRecDAO().findMachineStautByStautName("工作"));
			
			DataBean.MACHINESTAUTS = machineTotal;
			DataBean.NONET = (Integer)sc.getAttribute("nonet");
			DataBean.WITING = (Integer)sc.getAttribute("witing");
			DataBean.WORK = (Integer)sc.getAttribute("work");
		}
		
		//指标信息
		if(sc.getAttribute("indexconfigs") == null)
		{
			List<IndexConfig> indexs = this.getIndexConfigService().findAllIndexConfig();
			sc.setAttribute("indexconfigs", indexs);	
			Map<String,IndexConfig> configs = new HashMap<String,IndexConfig>();
			for(IndexConfig index:indexs)
			{
				configs.put(index.getIndexName(), index);
			}
			DataBean.CONFIGS = configs;
		}
		
		
		return mapping.findForward("success");
		
	}
	public ActionForward changePass(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UserForm uform = (UserForm)form;
		
		MacUser user = new MacUser();
		user.setUserName(uform.getUserName());
		user.setPassWord(uform.getPassWord());
		
		MacUser oldUser = this.getUserService().findOldUser(user);
		if(oldUser == null)
		{
			request.setAttribute("info", "用户名不存在 请重试！");
			return mapping.findForward("changesuccess");
		}
		if(!oldUser.getPassWord().equals(uform.getPassWord()))
		{
			request.setAttribute("info", "原密码错误 请重试！");
			return mapping.findForward("changesuccess");
		}
		user.setPassWord(uform.getNewPassWord());
		this.getUserService().updateUser(user);
		request.setAttribute("info", "密码修改成功！");
		return mapping.findForward("changesuccess");
		
		
	}
}
